package com.atguigu.ssyx.acl.service.impl;

import com.atguigu.ssyx.acl.mapper.RoleMapper;
import com.atguigu.ssyx.acl.service.AdminRoleService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.common.exception.GlobalExceptionHandler;
import com.atguigu.ssyx.model.acl.AdminRole;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private AdminRoleService adminRoleService;

    @Override
    public IPage<Role> selectRolePage(Page<Role> pageParam, RoleQueryVo roleQueryVo) {
//        获取条件数值
        String roleName = roleQueryVo.getRoleName();
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
//        判断条件是否为空,如果不为空则封装查询条件
        if (!StringUtils.isEmpty(roleName)) {
            wrapper.like(Role::getRoleName, roleName);
        }
//        调用方法实现条件分页查询
        IPage<Role> rolePage = baseMapper.selectPage(pageParam, wrapper);
//        返回分页对象
        return rolePage;
    }

    //    获取所有角色,和根据用户id查询用户分配
    @Override
    public Map<String, Object> getRoleByAdminId(Long adminId) {
//        1.查询所有角色
        List<Role> allRoleList = roleMapper.selectList(null);
//        2根据用户ID查询用户分配角色列表
//        2.1根据用户id查询 用户角色关系表 admin_role 查询用户关系分配角色id列表
        LambdaQueryWrapper<AdminRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminRole::getAdminId, adminId);
        List<AdminRole> list = adminRoleService.list(wrapper);
//        List<AdminRole>
//        2.2通过第一步返回集合,获取所有角色id的列表 List<Long>
        List<Long> collect = list.stream().map(item -> item.getRoleId()).collect(Collectors.toList());
//        2.3创建一个新的List集合,用户存储用户配置角色
        List<Role> assignRoleList = new ArrayList<>();
//        2.4遍历所有角色列表,allRolesList,得到角色
        for (Role role : allRoleList) {
//        判断所有角色里面是否包含分配角色的id,封装到2.3里面的新的list集合
            if (collect.contains(role.getId())) {
                assignRoleList.add(role);
            }
        }
//        封装到map,返回
        Map<String, Object> map = new HashMap<>();
        map.put("allRolesList", allRoleList);
        map.put("assignRoles", assignRoleList);
        return map;
    }

    //为用户进行分配
    @Override
    public void saveAdminRole(Long adminId, Long[] roleIds) {
//        1.删除用户已经分配过的角色数据
//        根据用户ID删除admin_role表里面对应的数据
        LambdaQueryWrapper<AdminRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminRole::getAdminId, adminId);
        if (adminRoleService.remove(wrapper)) {
//        2.重新进行分配权限
//        adminId:1 roleId:2 3
//        遍历多个角色ID,得到多个角色id,拿着每个角色的id+用户id添加用户角色关系表
            List<AdminRole> adminRoles = new ArrayList<>();
            for (Long roleId : roleIds) {
                AdminRole adminRole = new AdminRole();
                adminRole.setRoleId(roleId);
                adminRole.setAdminId(adminId);
                adminRoles.add(adminRole);
            }
            adminRoleService.saveBatch(adminRoles);
        } else {
            throw new RuntimeException("未获取到用户id");
        }

    }
}
